import { fileURLToPath, URL } from 'node:url'

import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { loadEnv, type ConfigEnv, type UserConfigExport } from 'vite'
// https://vitejs.dev/config/
export default (configEnv: ConfigEnv): UserConfigExport => {
  const viteEnv = loadEnv(configEnv.mode, process.cwd())

  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    base: './',
    server: {
      host: '0.0.0.0',
      port: 3333,
      cors: true,
      proxy: {
        [viteEnv.VITE_API_URL_API!]: {
          target: viteEnv.VITE_API_URL,
          changeOrigin: true,
          rewrite: (wPath) => wPath.replace(viteEnv.VITE_API_URL_API, '')
        },
        [viteEnv.VITE_API_BASE_URL_API!]: {
          target: viteEnv.VITE_API_BASE_URL,
          changeOrigin: true,
          rewrite: (wPath) => wPath.replace(viteEnv.VITE_API_BASE_URL_API, '')
        },
        [viteEnv.VITE_API_BJ_URL_API!]: {
          target: viteEnv.VITE_API_BJ_URL,
          changeOrigin: true,
          rewrite: (wPath) => wPath.replace(viteEnv.VITE_API_BJ_URL_API, '')
        },
        [viteEnv.VITE_API_ZJ_URL_API!]: {
          target: viteEnv.VITE_API_ZJ_URL,
          changeOrigin: true,
          rewrite: (wPath) => wPath.replace(viteEnv.VITE_API_ZJ_URL_API, '')
        }
      }
    }
  }
}
